#include "scr_state_title.h"
#include "scr_state_main_menu.h"
#include "scr_game.h"
#include "scr_resource_manager.h"
#include "scr_game_state_manager.h"
#include "scr_font.h"

SCRStateTitle::SCRStateTitle()
	: m_Time(0)
	, m_IsPressed(false)
{
}

void SCRStateTitle::OnInitialize()
{
	m_Font0 = m_pGame->GetResourceManager()->GetFont("fonts/appleberry_with_cyrillic.ttf", 72);
	m_Font1 = m_pGame->GetResourceManager()->GetFont("fonts/carbon.ttf", 28);
}

void SCRStateTitle::OnUpdate(const int& Delta)
{
	m_Time += Delta;
}

void SCRStateTitle::OnRender(const int& Delta)
{
	const int TitleTime = 5000;
	const int FlashTime = 2000;
	const int PressTime = 2000;
	const int TextCount = 8;
	const int TimePerText = 33;
	const char* Texts[TextCount] =
	{
		"HATE",
		"VIOLENCE",
		"DEATH",
		"APOCALYPSE",
		"DESTRUCTION",
		"DESPAIR",
		"ANGER",
		"GANGNAM"
	};

	const int TotalTime = TextCount * TimePerText; 
	const int TextIdx = (m_Time % TotalTime) / TimePerText;

	if (m_Time > TitleTime && m_Time < TitleTime + FlashTime)
	{
		float Factor = (float)(m_Time - TitleTime) / (float)FlashTime;
		float InvFactor = 1.0f - Factor;
		float Red = (255.0f * InvFactor);
		m_pGame->Clear((char)Red, 0, 0);
	}
	else
	{
		m_pGame->Clear();
	}

	m_Font0->SetColor(255, 0, 0);
	m_Font0->Draw(SCRGame::SCR_SCREEN_WIDTH >> 1, SCRGame::SCR_SCREEN_HEIGHT >> 1, Texts[TextIdx],
		SCRFontType::SOLID, SCRFontAlign::CENTER_X | SCRFontAlign::CENTER_Y);

	if (m_Time > TitleTime)
	{
		m_Font0->SetColor(255, 255, 255);
		m_Font0->Draw(SCRGame::SCR_SCREEN_WIDTH >> 1, SCRGame::SCR_SCREEN_HEIGHT >> 1, "THE SCARLET ROAD",
			SCRFontType::SOLID, SCRFontAlign::CENTER_X | SCRFontAlign::CENTER_Y);

		if (m_Time > TitleTime + FlashTime + PressTime)
		{
			m_Font1->SetColor(255, 255, 255);
			m_Font1->Draw(SCRGame::SCR_SCREEN_WIDTH >> 1, SCRGame::SCR_SCREEN_HEIGHT * 3 / 4, "PRESS ANY KEY TO CONTINUE",
				SCRFontType::SOLID, SCRFontAlign::CENTER_X | SCRFontAlign::CENTER_Y);
		}
	}
}

void SCRStateTitle::OnTerminate()
{
}

void SCRStateTitle::OnKeyClick(const int& Key)
{
	if (!m_IsPressed)
	{
		m_IsPressed = true;
		m_pGame->GetStateManager()->SwitchTo<SCRStateMainMenu>();
	}
}

void SCRStateTitle::OnMouseClick(const int& X, const int& Y, const SCRMouseButton::Enum& Button)
{
	if (!m_IsPressed)
	{
		m_IsPressed = true;
		m_pGame->GetStateManager()->SwitchTo<SCRStateMainMenu>();
	}	
}
